package org.dragonnova.meetingserver.model.provider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import org.dragonnova.meetingserver.model.bean.DeviceInfo;
import org.dragonnova.meetingserver.model.bean.MeetingInfo;
import org.dragonnova.meetingserver.model.bean.VoiceInfo;

/**
 * Created by Jeremy on 2016/1/4.
 */
public class RecordDBHelper extends SQLiteOpenHelper {

    private static final String TAG = RecordDBHelper.class.getName();

    public static final int DATABASE_VERSION = 1;

    public static final String DATABASE_NAME = "voice.db";
    public static final String MEETING_TABLE = "meeting";
    public static final String VOICE_TABLE = "voice";
    public static final String DEVICE_TABLE = "device";

    private static final String MEETING_TABLE_CREATE =
            "create table " + MEETING_TABLE + " ("
                    + MeetingInfo.KEY_ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + MeetingInfo.KEY_SERIAL_COLUMN + " INTEGER, "
                    + MeetingInfo.KEY_START_TIME_COLUMN + " INTEGER, "
                    + MeetingInfo.KEY_END_TIME_COLUMN + " INTEGER, "
                    + MeetingInfo.KEY_DURATION_COLUMN + " INTEGER, "
                    + MeetingInfo.KEY_NAME_COLUMN + " TEXT, "
                    + MeetingInfo.KEY_PASSWORD_COLUMN + " TEXT, "
                    + MeetingInfo.KEY_FLAG_COLUMN + " INTEGER);";

    private static final String VOICE_TABLE_CREATE =
            "create table " + VOICE_TABLE + " ("
                    + VoiceInfo.KEY_ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + VoiceInfo.KEY_ORG_TRAN_TEXT_COLUMN + " TEXT, "
                    + VoiceInfo.KEY_INSERT_DATE_COLUMN + " INTEGER, "
                    + VoiceInfo.KEY_TRAN_TEXT_COLUMN + " TEXT, "
                    + VoiceInfo.KEY_VOICE_TYPE_COLUMN + " TEXT, "
                    + VoiceInfo.KEY_VOICE_ORG_COST_TIME_COLUMN + " INTEGER, "
                    + VoiceInfo.KEY_VOICE_USER_ID_FOREIGN_COLUMN + " TEXT, "
                    + VoiceInfo.KEY_VOICE_FLAG_COLUMN + " INTEGER, "
                    + VoiceInfo.KEY_ORG_PATH_COLUMN + " TEXT); ";

    private static final String DEVICE_TABLE_CREATE =
            "create table " + DEVICE_TABLE + " ("
                    + DeviceInfo.KEY_ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + DeviceInfo.KEY_HOSTID_COLUMN + " TEXT, "
                    + DeviceInfo.KEY_HOSTNAME_COLUMN + " TEXT, "
                    + DeviceInfo.KEY_WIFIADDR_COLUMN + " TEXT, "
                    + DeviceInfo.KEY_WIFISTATUS_COLUMN + " INTEGER, "
                    + DeviceInfo.KEY_LANGUAGE_COLUMN + " TEXT, "
                    + DeviceInfo.KEY_PORT_COLUMN + " INTEGER, "
                    + DeviceInfo.KEY_FLAG_COLUMN + " INTEGER);";

    private static final String TABLE_DROP = "DROP DATABASE " + DATABASE_NAME;

    public RecordDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(MEETING_TABLE_CREATE);
        db.execSQL(VOICE_TABLE_CREATE);
        db.execSQL(DEVICE_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
        if (newVer > oldVer) {
            db.execSQL("DROP TABLE IF EXISTS " + MEETING_TABLE);
            db.execSQL("DROP TABLE IF EXISTS " + VOICE_TABLE);
            db.execSQL("DROP TABLE IF EXISTS " + DEVICE_TABLE);
            onCreate(db);
            Log.i(TAG, "Upgrading database from version " + oldVer + " to "
                    + newVer + ", which will destroy all old data");
        } else {
            Log.i(TAG, "No Upgrading database");
        }
    }
}
